/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.service.plugins.impl;

import java.util.List;

import org.grayrabbit.cms.dao.plugins.SubjectOptionDao;
import org.grayrabbit.cms.entity.SubjectOption;
import org.grayrabbit.cms.service.plugins.SubjectOptionService;
import org.grayrabbit.cms.vo.plugins.SubjectOptionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.service.impl.GenericServiceImpl;
/**
 * <b>OperLog</b>的service实现类，事务加此处：





 * @version 1.0,创建时间: 2011-05-16 16:09:05  
 */
@Service
@Transactional
public class SubjectOptionServiceImpl extends GenericServiceImpl implements SubjectOptionService {
	/**
	 * 注入dao层：
	 */
	@Autowired
	@Qualifier("subjectOptionDaoImpl")
	private SubjectOptionDao subjectOptionDao;
	
	/**
	 * 此方法执行时为只读事务：
	 * @see SubjectOptionService#getOperLog(String)
	 */
	@Transactional(readOnly=true)
	public SubjectOption getSubjectOption(final String optionId){
		SubjectOption subjectOption = subjectOptionDao.findById(optionId);
		subjectOptionDao.initialize(subjectOption);
		return subjectOption;
	}
	
	/**
	 * @see SubjectOptionService#delete(String)
	 */
	@Transactional
	public void delete(final SubjectOption subjectOption) {
		subjectOptionDao.makeTransient(subjectOption);
	}
	
	/**
	 * @see SubjectOptionService#save(OperLog)
	 */
	@Transactional
	public void save(final SubjectOption subjectOption) {
		subjectOptionDao.makePersistent(subjectOption);		
	}
	
	/**
	 * 此方法执行时为只读事务：
	 * @see SubjectOptionService#getPage(SubjectOptionBean)
	 */
	@Transactional(readOnly=true)
	public Page<SubjectOption> getPage(final SubjectOptionBean subjectOptionBean){
		Page<SubjectOption> page =  new Page<SubjectOption>();
		page.setTotal(subjectOptionDao.count(subjectOptionBean));
		page.setResult(subjectOptionDao.findByQc(subjectOptionDao.assembly(subjectOptionBean)));
		return page;
	}

	/**
	 * 此方法执行时为只读事务：
	 * @see SubjectOptionService#list(Page, SubjectOptionBean)
	 */
	public void list(Page<SubjectOption> page, SubjectOptionBean subjectOptionBean) {
		if(page==null)
			return;
		if(page.isAutoCount()){
			page.setTotal(subjectOptionDao.count(subjectOptionBean));
		}
		page.setResult(subjectOptionDao.findByPage(page,subjectOptionBean));
	}
	
	/**
	 * 根据主键id列删除，批量删除OperLog
	 * @see SubjectOptionService#delete(String[])
	 */
	public void delete(final String[] key) {
		int i =0;
		for(String id : key){
			subjectOptionDao.makeTransient(subjectOptionDao.findById(id));
			i++;
			if(i==MAX_CLEAR)
				subjectOptionDao.clear();
		}
		
	}
	/**
	 *根据科目Id查询答案项
	 * @param subjectId
	 * @return
	 */
	public List<SubjectOption> findBySubjectId(String subjectId){
		
		return subjectOptionDao.findBySubjectId(subjectId);
	}
}